home account info subscribe login search FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Bug Proofing Visual Basic: A Guide to Error Handling and Prevention
(Publisher: John Wiley & Sons, Inc.)
Author(s): Rod Stephens
ISBN: 0471323519
Publication Date: 11/01/98

Search this book:
 
Previous Table of Contents Next


Note that the background is smoothly shaded only on computers using high color graphics. If your computer is using 256 color mode, the background will be covered with large bands of color. It will still have the same bad performance, however.

Examine the code and figure out where it is spending all of its time. Use the Visual Basic Code Profiler (VBCP) if you like, or look at the timing results shown in Table 16.1. Then modify the code to improve the program’s performance without introducing other bugs.


Figure 16.1  Program Bad16 displaying a bar chart.

Option Explicit

Private Values(1 To 10) As Single

‘ Generate some random data and draw the graph.
Private Sub Form_Load()
   ‘ Load some random data.    
    LoadData    

   ‘ Shade the background.    
    ShadePicture
    
   ‘ Draw the graph.    
    DrawGraph
End Sub

   ‘ Create some random data.
   Private Sub LoadData()
   Dim i As Integer
   Dim min As Single
   Dim max As Single    
     
       Randomize    
     
       min = picGraph.ScaleHeight * 0.1    
       max = picGraph.ScaleHeight * 0.8    
       For i = 1 To 10        
              Values(i) = Int((max - min + 1) * Rnd + min)    
       Next i
   End Sub
     
   ‘ Shade picGraph from white at the top to
    ‘ black at the bottom.
    Private Sub ShadePicture()
    Const START_COLOR = 255
    Const END_COLOR = 0
    Dim Y As Single
    Dim clr As Single
    Dim d_clr As Single    
   
        d_clr = (END_COLOR - START_COLOR) / picGraph.ScaleHeight          clr = START_COLOR    
        For Y = 0 To picGraph.ScaleHeight        
            picGraph.Line (0, Y)- _            
                (picGraph.ScaleWidth, Y), _            
                RGB(clr, clr, clr)        
            clr = clr + d_clr    
        Next Y
    End Sub
     
   ‘ Draw the graph.
   Private Sub DrawGraph()
   Dim i As Integer
   Dim dx As Single
   Dim y0 As Single    
    
       y0 = picGraph.ScaleHeight * 0.9    
       dx = picGraph.ScaleWidth / 12
    
       ‘ Draw the bar chart.    
        For i = 1 To 10        
            picGraph.Line (i * dx, y0)-((i + 1) * dx, Values(i)), vbRed, BF    
        Next i    
        For i = 1 To 10        
            picGraph.Line (i * dx, y0)-((i + 1) * dx, Values(i)), vbBlack, B    
        Next i
    End Sub
Table 16.1 Function Timing Results for Program Bad16
Function Total Time Percent Time
Form_Load 0.000 0.00
DrawGraph 0.040 1.34
ShadePicture 2.941 98.66

Table 16.1 shows the function timing results given by VBCP for program Bad16 on a 133-megahertz Pentium. The table shows that program Bad16 spends almost all of its time in subroutine ShadePicture.

Study these results and the code to see if you can figure out where the program is wasting so much time, but be warned: The solution does not lie entirely within the code shown here. If you are an experienced Visual Basic programmer, you should be able to deduce the problem. If you are a beginner, do not spend too much time looking at the code before you read the solution in Appendix A, “Self-Test Solutions.”

Summary

Probably the most dangerous mistake programmers make when debugging is rushing through the job. They do not take the time to understand the context in which the code operates and they do not thoroughly test their changes.

Treat repair code just as you treat new code and follow the same guidelines. Use good coding practices and test the results. The Bug Stoppers that follow summarize the other debugging practices covered in this chapter.

BUG STOPPERS: debugging Habits
Think first, then act.
Debug when you are alert.
Debug using the same coding practices you use when writing any other code.
Fix your own bugs.
Fix bugs right away.
If you get stuck, take a break.
Add comments describing repairs.
If something suddenly breaks, see how the code has changed recently.
Don’t believe in magic.
Fix routines, not lines.
Fix bugs, not symptoms.
Look for multiple bugs causing one behavior.
Look for bugs hidden by other bugs.
Do not experiment.
Test fixes thoroughly.
Simplify code when you fix it.
Examine decisions closely.
Look for bugs where there have been bugs in the past.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.